#!/usr/bin/python
#coding: utf-8
import wx
import wx.lib.buttons
import wx.aui
import mytaskbar
import current
import paneltype
import KEHUXXDLG

menuid=[CPXX_INSERT,KHXX_INSERT,CPXX_DELETE,KHXX_DELETE,FENLEI_INSERT,FENLEI_DELETE]=[wx.NewId() for _init_ctrls in range(6)]
class mybook(wx.Frame):
    def __init__(self,parent):
        wx.Frame.__init__(self,parent)
        self.SetIcon(wx.Icon('1381love.ico',wx.BITMAP_TYPE_ANY))
        self.mainbox=wx.BoxSizer(wx.HORIZONTAL)
        self.panel=wx.Panel(self)
        self.mainbox.Add(self.panel,1,wx.EXPAND)
        
        self.showbox=wx.BoxSizer(wx.HORIZONTAL)
        self.panel.SetSizer(self.showbox)
        self.init_ctrl(self.showbox)    
        
        self.SetSizer(self.mainbox)
        self.SetClientSize(wx.Size(800,600))
        self.Center()
        self.makemenubar()
        self.MakeStatusBar()
        
        self.baricon=mytaskbar.mytaskbar(self)
        self.baricon.SetIcon(wx.Icon('1381love.ico',wx.BITMAP_TYPE_ANY))
        self.Bind(wx.EVT_CLOSE,self.OnClose)
    
        self.IsShow=True
    def OnClose(self,event):
        if self.IsShow:
            result=wx.MessageDialog(self.panel,u"是否退出?\n注意:选择 '取消'按钮 将最小化到托盘...").ShowModal()
            if result==wx.ID_OK:
                self.baricon.Destroy()
                self.Destroy()
            else:
                self.Hide()
            self.IsShow=False
        else:
            self.Hide()
      #  self.Show(False)
    def OnExit(self,event):
        wx.MessageBox(u'退出程序！')        
        self.baricon.Destroy()
        self.Destroy()
    def MakeStatusBar(self):        
        self.SetStatusBar(self.CreateStatusBar())
    
    def makemenubar(self):
        self.menubar=wx.MenuBar()
        self.menu1=wx.Menu()
        self.menu2=wx.Menu()
        self.fenlei=wx.Menu()
        
        self.menubar.Append(self.menu1,u'产口信息')
        self.menubar.Append(self.menu2,u'客户信息')
        self.menubar.Append(self.fenlei,u'分 类')
        
        self.menu1.Append(CPXX_INSERT,u'插入产品')
        self.menu2.Append(KHXX_INSERT,u'插入客户')
        self.menu1.Append(CPXX_DELETE,u'删除产品')
        self.menu2.Append(KHXX_DELETE,u'删除客户')
        self.fenlei.Append(FENLEI_INSERT,u'插入分类')
        self.fenlei.Append(FENLEI_DELETE,u'删除分类')
        
        """self.Bind(wx.EVT_MENU,self.cpxxinsert,id =CPXX_INSERT)
        self.Bind(wx.EVT_MENU,self.cpxxinsert,id =CPXX_DELETE)
        self.Bind(wx.EVT_MENU,self.cpxxinsert,id =KHXX_INSERT)
        self.Bind(wx.EVT_MENU,self.cpxxinsert,id =KHXX_DELETE)
        self.Bind(wx.EVT_MENU,self.cpxxinsert,id =FENLEI_INSERT)
        self.Bind(wx.EVT_MENU,self.cpxxinsert,id =FENLEI_DELETE)"""
        
        for id in menuid:
            self.Bind(wx.EVT_MENU,self.cpxxinsert,id=id)  
                   
        self.SetMenuBar(self.menubar)
    
    def cpxxinsert(self,event):
        id=event.GetId()
        if id==CPXX_INSERT:
            wx.MessageBox(u'插入产品'+str(id))
            import CPXXDLG
            #cpxxdlg=CPXXDLG.CPXX_DLG(self)
            #cpxxdlg.Show()
            cpxx=CPXXDLG.create(self)
            cpxx.ShowModal()
            self.Load_cpxx_panel_listctrl()
            self.itemID=-1
        elif id==CPXX_DELETE:
            wx.MessageBox(u'删除产品'+str(id))
            if self.itemID!=-1:
                current.DeleteOb(current.cpxx,'id',self.itemID)
            self.Load_cpxx_panel_listctrl()
        elif id==KHXX_INSERT:
            wx.MessageBox(u'插入客户'+str(id))            
            khxx=KEHUXXDLG.create(self)
            khxx.ShowModal()
            self.Load_kehuxx_panel_listctrl()
            self.itemID=-1
        elif id==KHXX_DELETE:
            wx.MessageBox(u'删除客户'+str(id))
            if self.itemID!=-1:
                current.DeleteOb(current.kehuxx,'name',self.itemID)                
            self.Load_kehuxx_panel_listctrl()
        elif id==FENLEI_INSERT:
            wx.MessageBox(u'插入分类'+str(id))
        elif id==FENLEI_DELETE:
            wx.MessageBox(u'删除分类'+str(id))
            
    def AuinotebookAddPanel(self,addpaneltype,lable,column):   #向self.auinotebook加入子项
            self.auinotebook.AddPage(addpaneltype,lable) 
            #strlist='listctr_'+ addpaneltype.__str__()
            
            #print strlist
            def insertcolumn(column):  
                if addpaneltype.type_name=='cpxx_panel':
                    self.listctr_cpxx=wx.ListCtrl(self.cpxx_panel,size=(400,300),style=wx.LC_REPORT)   
                    con=0
                    for i in column:
                        self.listctr_cpxx.InsertColumn(con,i)
                        con+=1              
                    
                    box=wx.BoxSizer()
                    box.Add(self.listctr_cpxx,1,wx.EXPAND,0)
                    self.cpxx_panel.SetSizer(box) 
                elif addpaneltype.type_name=='kehuxx_panel':                    
                    self.listctr_kehuxx=wx.ListCtrl(self.kehuxx_panel,size=(400,300),style=wx.LC_REPORT)   
                    con=0
                    for i in column:
                        self.listctr_kehuxx.InsertColumn(con,i)
                        con+=1     
                    box=wx.BoxSizer()
                    box.Add(self.listctr_kehuxx,1,wx.EXPAND,0)
                    self.kehuxx_panel.SetSizer(box)
                    
            insertcolumn(column)    
            
    def init_ctrl(self,parent):
        self.leftbox=wx.BoxSizer(wx.VERTICAL)
        self.rightbox=wx.BoxSizer(wx.VERTICAL)
        parent.Add(self.leftbox,0,wx.EXPAND)
        parent.Add(self.rightbox,1,wx.EXPAND)        
        
        self.auinotebook=wx.aui.AuiNotebook(self.panel)
        self.cpxx_panel=paneltype.my_panel(self.auinotebook,'cpxx_panel')
        #self.auinotebook.AddPage(self.cpxx_panel,u'产品信息')
        cpxx_column=[u'产品ID',u'产品名称',u'产品编号',u'产品价格',u'产品成本']  
        self.AuinotebookAddPanel(self.cpxx_panel,u'产品信息',cpxx_column)
        
        kehuxx_column=[u'姓名',u'电话',u'地址',u'公司名称']
        
        #self.kehuxx_panel=wx.Panel(self.auinotebook)  
        self.kehuxx_panel=paneltype.my_panel(self.auinotebook,'kehuxx_panel')  
        self.AuinotebookAddPanel(self.kehuxx_panel,u'客户信息',kehuxx_column)       
        
        #
        
        
        """
        #cpxx_column=[u'产品ID',u'产品名称',u'产品编号',u'产品价格',u'产品成本']
        #这一段全部由self.AuinotebookAddPanel(self.cpxx_panel,u'产品信息',cpxx_column)代替
        #内嵌函数def insertcolumn(column): for i in column实现
        self.listctr_cpxx=wx.ListCtrl(self.cpxx_panel,size=(400,300),style=wx.LC_REPORT)
        self.listctr_cpxx.InsertColumn(0,u'产品ID')
        self.listctr_cpxx.InsertColumn(1,u'产品名称')
        self.listctr_cpxx.InsertColumn(2,u'产品编号')
        self.listctr_cpxx.InsertColumn(3,u'产品价格')
        self.listctr_cpxx.InsertColumn(4,u'产品成本')
        box=wx.BoxSizer()
        box.Add(self.listctr_cpxx,1,wx.EXPAND,0)
        self.cpxx_panel.SetSizer(box)   """     
        
        
        self.gen1 = wx.lib.buttons.GenBitmapTextToggleButton(bitmap=wx.Bitmap('down_stack_24.png'),id=-1,
               label=u'库存',
               name='genBitmapTextToggleButton1', parent=self.panel,style=0)

        self.gen2 = wx.lib.buttons.GenBitmapTextToggleButton(bitmap=wx.Bitmap('down_stack_24.png'),id=-1,
               label=u'产品',
               name='genBitmapTextToggleButton2', parent=self.panel,style=0)

        
        self.leftbox.Add(self.gen1,0,wx.EXPAND|wx.LEFT|wx.ALL,3)
        self.leftbox.Add(self.gen2,0,wx.EXPAND|wx.LEFT|wx.ALL,3)        
        
        self.rightbox.Add(self.auinotebook,1,wx.EXPAND)
                
        self.Load_cpxx_panel_listctrl()        
        """for i in f:            
            mingch=i.mingch
            key=i.key
            index=self.listctr_cpxx.InsertStringItem(0,'a')
            self.listctr_cpxx.SetStringItem(index,0,mingch)
            self.listctr_cpxx.SetStringItem(index,1,key)"""
        
        self.Load_kehuxx_panel_listctrl()
        
        self.listctr_cpxx.Bind(wx.EVT_LIST_ITEM_SELECTED,self.OnListCtrl_cpxx_ListItemSelected)
        self.listctr_cpxx.Bind(wx.EVT_LEFT_DCLICK, self.OnListCtrl_cpxx_LDclick)
        
        self.listctr_kehuxx.Bind(wx.EVT_LIST_ITEM_SELECTED,self.OnListCtrl_kehuxx_ListItemSelected)
        self.listctr_kehuxx.Bind(wx.EVT_LEFT_DCLICK,self.OnListCtrl_kehuxx_LDclick)
        
    def OnListCtrl_kehuxx_LDclick(self,event):
        if self.khcurrentItem!=-1:#self.currentItem = event.m_itemIndex      
            #self.khxxIDb = self.listctr_kehuxx.GetItem(self.khcurrentItem,0).Text
            name = self.listctr_kehuxx.GetItem(self.khcurrentItem,0).Text
            phone = self.listctr_kehuxx.GetItem(self.khcurrentItem,1).Text
            addr = self.listctr_kehuxx.GetItem(self.khcurrentItem,2).Text
            gsname = self.listctr_kehuxx.GetItem(self.khcurrentItem,3).Text
            wx.MessageBox(str(self.khcurrentItem))     
            khxx=KEHUXXDLG.create(self)
            KEHUXXDLG.KEHUXX_DLG.modify=True
            khxx.name.SetValue(name)
            khxx.phone.SetValue(phone)
            khxx.addr.SetValue(addr)
            khxx.gsname.SetValue(gsname)  
            khxx.findname=name
            khxx.ShowModal()
            self.Load_kehuxx_panel_listctrl()
            self.khcurrentItem=-1
            self.itemID=-1
    
    def OnListCtrl_kehuxx_ListItemSelected(self,event): #在列表中左键点击后，选中项的事件
        self.khcurrentItem = event.m_itemIndex    
        self.itemID = self.listctr_kehuxx.GetItem(self.khcurrentItem,0).Text
        item = self.listctr_kehuxx.GetItem(self.khcurrentItem,1).Text
        #wx.MessageBox(item,self.itemID)
        
        
    def OnListCtrl_cpxx_LDclick(self,event):  #双击左键产生事件，用于修改数据
        if self.currentItem!=-1:#self.currentItem = event.m_itemIndex      
            self.itemIDb = self.listctr_cpxx.GetItem(self.currentItem,0).Text
            mingchb = self.listctr_cpxx.GetItem(self.currentItem,1).Text
            keyb = self.listctr_cpxx.GetItem(self.currentItem,2).Text
            jiageb = self.listctr_cpxx.GetItem(self.currentItem,3).Text
            chengbenb = self.listctr_cpxx.GetItem(self.currentItem,4).Text
            wx.MessageBox(str(self.currentItem))     
            wx.MessageBox('listctrl_cpxx_LDclick')
            import CPXXDLG
            cpxx=CPXXDLG.create(self)
            CPXXDLG.CPXX_DLG.modify=True
            cpxx.cpmc_tc.SetValue(mingchb)
            cpxx.cpdm_tc.SetValue(keyb)
            cpxx.cpjiage_tc.SetValue(jiageb)
            cpxx.cpcb_tc.SetValue(chengbenb)
            cpxx.tempmingch=self.itemID
            
            cpxx.ShowModal()
            self.Load_cpxx_panel_listctrl()
            self.currentItem=-1
            self.itemID=-1
        
        
        
    def OnListCtrl_cpxx_ListItemSelected(self,event):  #在列表中左键点击后，选中项的事件
        self.currentItem = event.m_itemIndex
        #wx.MessageBox(str(self.currentItem))
        #self.SetItemState(self.currentItem,self.SetBackgroundColour("yellow"),wx.LIST_STATE_SELECTED)
        self.itemID = self.listctr_cpxx.GetItem(self.currentItem,0).Text
        item = self.listctr_cpxx.GetItem(self.currentItem,1).Text
        #wx.MessageBox(item,self.itemID)
        
        
                
    def Load_cpxx_panel_listctrl(self):      #加载列表信息  
        self.listctr_cpxx.DeleteAllItems()
        #s=current.CreateSession()
        #lists=s.query(current.cpxx)
        lists=current.SelectOb(current.cpxx)
        count=0
        for i in lists:
            nID=str(i.id)
            mingch=i.mingch            
            key=i.key
            jiage=i.jiage
            chengben=i.chengben
            index=self.listctr_cpxx.InsertStringItem(count,'a')
            self.listctr_cpxx.SetStringItem(index,0,nID)
            self.listctr_cpxx.SetStringItem(index,1,mingch)
            self.listctr_cpxx.SetStringItem(index,2,key)
            self.listctr_cpxx.SetStringItem(index,3,jiage)
            self.listctr_cpxx.SetStringItem(index,4,chengben)            
            count=count+1
    def Load_kehuxx_panel_listctrl(self):     #加载客户信息到列表中
        self.listctr_kehuxx.DeleteAllItems()
        lists=current.SelectOb(current.kehuxx)
        for i in lists:
            nID=str(i.id)
            index=self.listctr_kehuxx.InsertStringItem(0,'b')
            self.listctr_kehuxx.SetStringItem(index,0,i.name)
            self.listctr_kehuxx.SetStringItem(index,1,i.phone)
            self.listctr_kehuxx.SetStringItem(index,2,i.addr)
            self.listctr_kehuxx.SetStringItem(index,3,i.gsname)
        
        

if __name__ == '__main__':       
    app=wx.PySimpleApp()
    showmain=mybook(None)
    showmain.Show()
    app.MainLoop()
        